package org.xiaom.butler.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.xiaom.butler.R;
import org.xiaom.butler.bean.Content;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.dbService.ContentService;
import org.xiaom.butler.dbService.DepartmentService;
import org.xiaom.butler.dbService.HotelService;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class DataManager { 
	public static Hotel getDataAndSave(Context context) {
		SharedPreferences sp=context.getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		String urlPrefix=NetHelper.getRootUrl(context);
		String path=context.getString(R.string.app_image_path);
		NetHelper helper=new NetHelper();
		FileHelper fileHelper=new FileHelper(context, path);
		Hotel hotel=getHotel(context);
		
		HotelService hotelService=new HotelService(context);
		DepartmentService departmentService=new DepartmentService(context);
		ContentService contentService=new ContentService(context);
		List<Hotel> hotels=new ArrayList<Hotel>();
		hotels.add(hotel);
		String imagePath=fileHelper.createSDFile(System.currentTimeMillis()+".gif", helper.getInputStream(urlPrefix+hotel.getImagePath()));
		if(imagePath!=""){
			hotel.setImagePath(imagePath);
		}
		
		List<Department> departments=hotel.getDepartments();
		for(Department d:departments){
			d.setHotel(hotel);
			imagePath=fileHelper.createSDFile(System.currentTimeMillis()+".gif", helper.getInputStream(urlPrefix+d.getImagePath()));
			if(imagePath!=""){
				d.setImagePath(imagePath);
			}
			List<Content> contents=d.getContents();
			for(Content c:contents){
				c.setDepartment(d);
				imagePath=fileHelper.createSDFile(System.currentTimeMillis()+".gif", helper.getInputStream(urlPrefix+c.getImagePath()));
				if(imagePath!=""){
					c.setImagePath(imagePath);
				}
			}
		}
		
		hotelService.insert(hotels);
		sp.edit().putString(AppHelper.SP_CURRENT_HOTEL_PHONE,hotel.getPhone());
		sp.edit().commit();
		departmentService.insert(hotel.getDepartments());
		for(Department d:departments){
			contentService.insert(d.getContents());
		}
		
		return hotel;
	} 

	public static void clearAll(Context context) {
		HotelService hotelService=new HotelService(context);
		DepartmentService departmentService=new DepartmentService(context);
		ContentService contentService=new ContentService(context); 
		List<Hotel> hotels=hotelService.get(null);
		List<Department> departments;
		List<Content> contents;
		hotelService.deleteAll();
		File f;
		try {
			for (Hotel h : hotels) {
				f = new File(h.getImagePath());
				if(f.exists()){
					f.delete();
				}
				departments=departmentService.getByHotel(h.getId().toString());
				for(Department d:departments){
					f = new File(d.getImagePath());
					if(f.exists()){
						f.delete();
					}
					contents=contentService.getByDepartment(d.getId().toString());
					for(Content c:contents){
						f = new File(c.getImagePath());
						if(f.exists()){
							f.delete();
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean checkUpdate(Context context) {
		Hotel h=new HotelService(context).getCurrentHotel();
		Hotel newHotel=getHotel(context);
		return newHotel.getVersion()>h.getVersion();
	}
	private static Hotel getHotel(Context context){
		SharedPreferences sp=context.getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		NetHelper helper=new NetHelper();
		String jsonStr = helper.getString(NetHelper.getRootUrl(context)+ "/app/get.and?hotel="+sp.getString(AppHelper.SP_CURRENT_HOTEL, "1"));
		Gson gson = new GsonBuilder().create();
		Hotel hotel=gson.fromJson(jsonStr, Hotel.class);
		return hotel;
	}
}
